<?php

class Employee extends IController
{
    public function lists()
    {
        $page = IReq::get('page') ? IFilter::act(IReq::get('page'), 'int') : 1;
        $employeeObj = new IQuery('employee');
        $employeeObj->where = "deleted_at IS NULL";
        $employeeObj->page = $page;
        $employeeObj->order = "created_at DESC";
        $employeeData = $employeeObj->find();
        JSON::jsonSuccess([
            'employeeObj' => $employeeObj->paging,
            'employeeData' => $employeeData
        ]);
    }

    /**
     * 从业人员添加
     */
    public function add()
    {
        if (IReq::getMethod() === 'POST') {
            
            $reqTime = ITime::getDateTime(); //请求时间

            // 准备入库数据（匹配saas_employee字段）
            $params_data = [
                'id' => Common::create_uuid(), // 生成36位UUID
                'operator_id' => IFilter::act(IReq::input('operator_id')),
                'school_id' => IFilter::act(IReq::input('school_id')),
                'name' => IFilter::act(IReq::input('name')),
                'employee_number' => IFilter::act(IReq::input('employee_number')),
                'position' => IFilter::act(IReq::input('position')),
                'avatar' => IFilter::act(IReq::input('avatar')),
                'introduction' => IFilter::act(IReq::input('introduction')),
                'status' =>  IFilter::act(IReq::input('status', 1), 'int'),
                'created_at' => $reqTime,
                'updated_at' => $reqTime,
                'deleted_at' => null
            ];

            // 验证必填参数
            $requiredFields = ['name', 'employee_number', 'school_id']; // 根据实际需求调整必填字段
            $validateResult = $this->validateRequiredFields($params_data, $requiredFields);
            if (!$validateResult['status']) {
                JSON::jsonErr(100002, $validateResult['message']);
            }  

            try {
                // 插入从业人员数据
                $employeeObj = new IModel('employee');
                $employeeObj->setData($params_data);
                $employeeObj->add();
                JSON::jsonSuccess();
            } catch (\Exception $e) {
                JSON::jsonErr(100002,'',['return_code'=>$e->getCode(),'return_msg'=>$e->getMessage()]);
            }
        } else {
            JSON::jsonErr(405);
        }
    }

    public function edit()
    {
        if (IReq::getMethod() === 'POST') {
            
            $reqTime = ITime::getDateTime(); //请求时间

            // 准备入库数据（匹配saas_employee字段）
            $params_data = [
                'id' => IFilter::act(IReq::input('id')),
                'operator_id' => IFilter::act(IReq::input('operator_id')),
                'school_id' => IFilter::act(IReq::input('school_id')),
                'name' => IFilter::act(IReq::input('name')),
                'employee_number' => IFilter::act(IReq::input('employee_number')),
                'position' => IFilter::act(IReq::input('position')),
                'avatar' => IFilter::act(IReq::input('avatar')),
                'introduction' => IFilter::act(IReq::input('introduction')),
                'status' =>  IFilter::act(IReq::input('status', 1), 'int'),
                'created_at' => $reqTime,
                'updated_at' => $reqTime,
                'deleted_at' => null
            ];

            // 验证必填参数
            $requiredFields = ['name', 'employee_number', 'school_id']; // 根据实际需求调整必填字段
            $validateResult = $this->validateRequiredFields($params_data, $requiredFields);
            if (!$validateResult['status']) {
                JSON::jsonErr(100002, $validateResult['message']);
            }  
            
            try {
                // 插入从业人员数据
                $employeeObj = new IModel('employee');
                $employeeObj->setData($params_data);
                $result = $employeeObj->update("id='".$params_data['id']."'");
                JSON::jsonSuccess($result);
            } catch (\Exception $e) {
                JSON::jsonErr(100002,'更新失败',['return_code'=>$e->getCode(),'return_msg'=>$e->getMessage()]);
            }
        } else {
            JSON::jsonErr(405);
        }
    }

    public function delete()
    {
        $id = IReq::input('id');

        $params_data = [
            'deleted_at' => ITime::getDateTime(), //请求时间
        ];

        try {
            // 插入从业人员数据
            $employeeObj = new IModel('employee');
            $employeeObj->setData($params_data);
            $result = $employeeObj->update("id='".$id."'");
            JSON::jsonSuccess($result);
        } catch (\Exception $e) {
            JSON::jsonErr(100002,'更新失败',['return_code'=>$e->getCode(),'return_msg'=>$e->getMessage()]);
        }
    }

    /**
     * 手动验证必填字段
     * @param array $data 请求参数
     * @param array $fields 必填字段列表
     * @return array 验证结果（status:bool, message:string）
     */
    private function validateRequiredFields($data, $fields) {
        foreach ($fields as $field) {
            if (!isset($data[$field]) || empty(trim($data[$field]))) {
                return ['status' => false, 'message' => "字段 {$field} 为必填项"];
            }
        }
        return ['status' => true, 'message' => '验证通过'];
    }
}
